package com.zhixi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhixi.config.CacheNameTimeConstant;
import com.zhixi.mapper.UserMapper;
import com.zhixi.pojo.User;
import com.zhixi.service.UserService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zhangzhixi
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2022-05-01 12:15:07
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 添加缓存
     */
    @Override
    @Cacheable(key = "#userId", value = CacheNameTimeConstant.CACHE_10SECS, unless = "#result==null")
    public User selectByIdUser(Long userId) {
        return userMapper.selectById(userId);
    }

    /**
     * 更新缓存
     */
    @Override
    @CachePut(key = "#user.id", value = CacheNameTimeConstant.CACHE_10SECS, unless = "#result==null")
    public User updateUser(User user) {
        return userMapper.updateById(user) == 1 ? user : null;
    }

    /**
     * 删除缓存
     */
    @Override
    @CacheEvict(key = "#userId", value = CacheNameTimeConstant.CACHE_10SECS)
    public boolean delUser(Long userId) {
        return userMapper.deleteById(userId) == 1;
    }
}
